<template>
  <div class="site-wrapper">
    <div class="header clearfix">
      <el-button size="small" type="primary" @click="openEmailDialog(false)" v-if="haslimit">写邮件</el-button>
      <div class="filter" @click="showFilter = true;"><img src="../../../../assets/livolobim/filter.png" alt="">搜索</div>
      <div class="mask" v-if="showFilter" @click="showFilter = false;"></div>
      <div class="filter-wrapper" v-if="showFilter">
        <span class="filter-arrow"></span>
        <span class="filter-white"></span>
        <div class="filter-header">搜索</div>
        <el-form :model="filterData" style="padding: 0 16px;" label-width="80px">
          <el-form-item label="发件人">
            <el-input v-model="filterData.sender"></el-input>
          </el-form-item>
          <el-form-item label="收件人">
            <el-input v-model="filterData.messageTo"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="filterData.status" style="width: 100%;"  placeholder="请选择">
              <el-option v-for="(item, index) in status" :key="index" :label="item" :value="index"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button style="float:right;margin-right: 16px;" size="small" type="primary" @click="searchItem(true)">确定</el-button>
          <el-button style="float:right;margin-right: 16px;" size="small" @click="resetFilter">重置</el-button>
        </div>
      </div>
    </div>
    <div class="data-wrapper">
      <el-table :height="tableHeight" :highlight-current-row="true" :data="tableData" border style="width: 100%">
        <el-table-column align="center" type="index" label="序号" width="55"></el-table-column>
        <el-table-column align="center" :show-overflow-tooltip="true" prop="sender" label="发送人" min-width="100"></el-table-column>
        <el-table-column align="center" :show-overflow-tooltip="true" prop="messageTo" label="收件人" min-width="100"></el-table-column>
        <el-table-column align="center" :show-overflow-tooltip="true" prop="subject" label="主题" min-width="180"></el-table-column>
        <el-table-column align="center" prop="createdAt" label="创建日期" width="100">
          <template slot-scope="scope">{{!scope.row.createdAt ? '-' : scope.row.createdAt.substring(0, 10)}}</template>
        </el-table-column>
        <el-table-column align="center" label="发送时间" width="160">
          <template slot-scope="scope">{{!scope.row.sendDate ? scope.row.createdAt.substring(0, 16) : scope.row.sendDate.substring(0, 16)}}</template>
        </el-table-column>
        <el-table-column align="center" :show-overflow-tooltip="true" prop="status" label="状态" width="120">
          <template slot-scope="scope">{{status[scope.row.status]}}</template>
        </el-table-column>
        <el-table-column align="center" label="操作" min-width="100">
          <template slot-scope="scope"><el-button @click="viewEmail(scope.row)" type="text" style="padding: 0;">查看邮件</el-button></template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="page"
        :page-sizes="[15, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>
    <Email ref="email" @refresh="searchItem(true)" />
  </div>
</template>

<script>
import dingMixin from "@/mixins/ding";
import formUtil from "@/utils/formUtil";
import Email from './Email.vue'
import {Loading, Message} from 'element-ui'

export default {
  data() {
    return {
      showFilter: false,
      loading:false,
      tableHeight: window.tableHeight,
      tableData: [],
      page:1,
      pageSize: 15,
      totalPage: 1,
      filterData: {
        sender: '',
        messageTo: '',
        status: ''
      },
      status: ['未发送', '发送成功', '发送失败', '多次发送失败，不再发送'],
    }
  },
  components: {
    Email
  },
  mixins: [dingMixin],
  computed: {
    haslimit() {
      return this.userInfo.roleIdList && (this.userInfo.roleIdList.includes('1311210362356432897') || this.userInfo.roleIdList.includes('1328502655740379138'))
    },
  },
  async mounted() {
    this.$nextTick(() => {
      this.tableHeight = document.documentElement['clientHeight'] - 170;
    })
    window.onresize = () => {
      this.tableHeight = document.documentElement['clientHeight'] - 170;
    }
    this.searchItem(true);
  },
  methods: {
    openEmailDialog(data) {
      this.$refs.email.openEmail(data);
    },
    resetFilter() {
      this.filterData = {
        sender: '',
        messageTo: '',
        status: ''
      }
      this.searchItem(true);
    },
    sizeChangeHandle(limit) {
      this.pageSize = limit;
      this.searchItem(true);
    },
    currentChangeHandle(page) {
      this.page = page;
      this.searchItem();
    },
    async searchItem(init) {
      if(init) this.page = 1;
      let {data: res} = await this.$http.post('bms/mail/record/list?pageNum='+this.page+'&pageSize='+this.pageSize,{
        sender: this.filterData.sender,
        messageTo: this.filterData.messageTo,
        status: this.filterData.status
      })
      if (res.resultCode !== "000") return this.$message.error(res.resultMessage);
      this.tableData = res.data.list
      this.totalPage = Number(res.data.total)
    },
    async viewEmail(row) {
      this.$refs.email.openEmail(row, row, 'view');
    }
  }
}
</script>

<style lang="scss" scoped>
  .header{
    position: relative;
    padding: 10px 0 0 20px ;
    .filter{
      cursor: pointer;
      float: right;
      margin-right: 30px;
      line-height: 30px;
      img{
        width: 16px;
        margin-right: 3px;
      }
    }
    .mask{
      position: fixed;
      height: 100vh;
      width: 100%;
      top: 0;
      right: 0;
      z-index: 3;
    }
    .filter-wrapper{
      position: absolute;
      width: 350px;
      padding-bottom: 20px;
      background: white;
      top: 48px;
      right: 20px;
      z-index: 99;
      border-radius: 5px;
      box-shadow: 0 0 3px black;
      .filter-header{
        line-height: 40px;
        padding-left: 16px;
        border-bottom: 1px solid #DDD;
        margin-bottom: 20px;
      }
      .filter-arrow{
        width:12px;
        height:12px;
        position: absolute;
        top: -7px;
        right: 20px;
        box-shadow: 0 0 3px black;
        transform: rotate(45deg);
      }
      .filter-white{
        width:18px;
        height:18px;
        position: absolute;
        top: -2px;
        right: 17px;
        border-radius: 3px;
        background: white;
      }
    }
  }
  .data-wrapper{
    padding: 0 20px;
    margin-top: 10px;
  }
</style>